<?php
/**
 * Description of AdminDAO
 *
 * @author Magno
 */
class AdminDAO extends UsuarioDAO{
    
    protected $conexao;
    protected $mediaDAO;
    protected $statusDAO;
    private $join = " SELECT * FROM admin a INNER JOIN usuario u ON a.usuarioID = u.usuarioID ";

    public function __construct() {
        parent::__construct();
        $this->conexao = Conexao::getConexao();
    }

    public function salvar($admin) {

        try {

            $admin->setUsuario(parent::salvar($admin->getUsuario()));

            if($this->existe($admin->getUsuarioID())) {
                return $admin;
            }else {
            
                $statment = $this->conexao->prepare("INSERT INTO admin(usuarioID) VALUES (".$admin->getUsuarioID().");");
            }
            
            if($statment->execute()) {
                return $admin;
            }else {
                $erros = $statment->errorInfo();
                throw new Exception("Erro AdminDAO: ".$erros[2]);
            }
            
        }catch(PDOException $err) {
            throw new Exception("Erro AdminDAO:".$err->getMessage());
        }

    }

    public function excluir($usuarioID) {

        try {
            parent::excluir($usuarioID);
        }catch(PDOException $err) {
            throw new Exception("Erro AdminDAO:".$err->getMessage());
        }

    }

    public function listar($pagina = 0, $email = "", $login = "", $statusID = -1, $carregarDependencias = false) {
        try {

            $limite = Util::getPaginacao($pagina);
            $where = parent::getWhere($email, $login, $statusID);
            $admins = array();

            $statment = $this->conexao->query( $this->join.$where. " $limite " );

            foreach ( $statment->fetchAll(PDO::FETCH_SERIALIZE) as $row ) {
                $admin = new AdminBean($row['usuarioID'], $row['login'], $row['senha'], $row['email'], $row['ip'], Util::dateTimeToString($row['dataCriacao']));
                $admin->setStatus($this->statusDAO->buscarPorID($row['statusID']));

                if($carregarDependencias)
                    $admin->setMedia($this->mediaDAO->buscarPorID($row['mediaID']));
                $admins[] = $admin;
            }

            return $admins;

        }catch(PDOException $err) {
            throw new Exception("Erro AdminDAO:".$err->getMessage());
        }
    }

    public function buscarPorID($usuarioID, $carregarDependencias = false) {
        try {

            $row = $this->conexao->query( $this->join." WHERE a.usuarioID = $usuarioID ")->fetch(PDO::FETCH_SERIALIZE);

            if(isset($row['usuarioID']) && $row['usuarioID'] > 0) {
                $admin = new AdminBean($row['usuarioID'], $row['login'], $row['senha'], $row['email'], $row['ip'], Util::dateTimeToString($row['dataCriacao']));
                $admin->setStatus($this->statusDAO->buscarPorID($row['statusID']));

                if($carregarDependencias)
                    $admin->setMedia($this->mediaDAO->buscarPorID($row['mediaID']));
                return $admin;
            }else {
                return null;
            }


        }catch(PDOException $err) {
            throw new Exception("Erro AdminDAO:".$err->getMessage());
        }
    }

    public function existe($usuarioID){
        if($this->buscarPorID($usuarioID) == null)
            return false;
        return true;
    }

    public function total($email = "", $login = "", $statusID = -1) {
        try {

            $where = parent::getWhere($email, $login, $statusID);
            $total = $this->conexao->query( " SELECT COUNT(a.usuarioID) FROM admin a INNER JOIN usuario u ON a.usuarioID = u.usuarioID " .$where )->fetch(PDO::FETCH_COLUMN);
            return $total;

        }catch(PDOException $err) {
            throw new Exception("Erro AdminDAO:".$err->getMessage());
        }
    }
}
?>
